package com.hqins.app.cloud.common;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    @Resource
    ResponseObject baseValueObject;

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        baseValueObject.setSuccess(false);
        if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
            baseValueObject.setStatus(404);
        } else {
            baseValueObject.setStatus(200);
        }
        baseValueObject.setMessage(e.getLocalizedMessage());
        return baseValueObject;
    }
}
